<?php
/*
 * form to add a new account to the system
 */

// include the page header
include ("inc_files/ui/header.php");
?>
<title>New user</title>
<script type="text/javascript">

function validateForm(){
	// reset the styles on the form fields to remove
	// any highlighted errors from previous submissions
	var nuf=document.getElementById("newuserform");
	for (var i=0;i<nuf.length;i++)
	  {
		  if(nuf.elements[i].className = "tberror") nuf.elements[i].className = "tbnormal"
	  }

	//check form validation
	var valid = true;
	var errormessage = "This form is not valid. Please correct the following errors.\n";
	
	if(document.getElementById('firstname').value.length < 1){
		valid=false;
		document.getElementById('firstname').className = "tberror";
		errormessage += "\nFirst name field empty";
	}
	if(document.getElementById('lastname').value.length < 1){
		valid=false;
		document.getElementById('lastname').className = "tberror";
		errormessage += "\nLast name field empty";
	}
	if(document.getElementById('email').value.length < 1){
		valid=false;
		document.getElementById('email').className = "tberror";
		errormessage += "\nEmail field empty";
	}
	//validate the password for length, complexity and matching confirm passord
	var password = document.getElementById('password').value;
	var passwordreg =   /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9!@#$%^&*\]\[\(/)]{7,32}$/;
	if(password.length < 7 && password.length > 0){
		valid=false;
		document.getElementById('password').className = "tberror";
		errormessage += "\nPassword too short";
	} else if(!password.match(passwordreg)){
		valid=false;
		document.getElementById('password').className = "tberror";		
		errormessage += "\nPassword too simple.  Please include at least 1 of each of the following:";
		errormessage += "\ndigit, lower case letter and upper case letter.";
		errormessage += "\nPassowrds can only contain characters a-z A-Z 0-9 !@#$%^&*[]()";		
	}
	if(password != document.getElementById('confirmpassword').value){
		valid=false;
		document.getElementById('password').className = "tberror";
		document.getElementById('confirmpassword').className = "tberror";
		errormessage += "\nPasswords don't match";
	}
	
	if(valid == false){
		alert(errormessage);
		
	}
	return valid;
}
</script>
</head>
<body>
	<?php include("inc_files/ui/menu.php");?>
	<form id="newuserform" action="m_adduser.php" method="post"
		onsubmit="return validateForm()">
		<table class="inputform">
			<tr>
				<td class="labelcell">First name:</td>
				<td class="formcell"><input type="text" id="firstname"
					name="firstname" size="54" class="tbnormal" /></td>
			</tr>
			<tr>
				<td class="labelcell">Last name:</td>
				<td class="formcell"><input type="text" id="lastname"
					name="lastname" size="54" class="tbnormal" /></td>
			</tr>

			<tr>
				<td class="separatorrow" colspan="2"><hr /></td>

			</tr>

			<tr>
				<td class="labelcell">Email:</td>
				<td class="formcell"><input type="text" name="email" id="email"
					size="54" class="tbnormal" /></td>
			</tr>

			<tr>
				<td class="labelcell">Password:</td>
				<td class="formcell"><input type="password" id="password"
					name="password" size="54" class="tbnormal" /></td>
			</tr>
			<tr>
				<td class="labelcell">Comfirm password:</td>
				<td class="formcell"><input type="password" id="confirmpassword"
					name="confirmpassword" size="54" class="tbnormal" /></td>
			</tr>
			<tr>
				<td class="separatorrow" colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" value="Create Account" /></td>
			</tr>
		</table>
	</form>
	<?php 	include("inc_files/ui/footer.php");?>
</body>
</html>
